package com.tzh.plugin.utils;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.VisitableElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public class PluginSqlAnnotationUtils {

    public static void commentResultMap(XmlElement element, IntrospectedTable introspectedTable) {
        List<VisitableElement> es = element.getElements();
        if (!es.isEmpty()) {
            String alias = introspectedTable.getTableConfiguration().getAlias();
            int aliasLen = -1;
            if (alias != null) {
                aliasLen = alias.length() + 1;
            }

            Iterator<VisitableElement> it = es.iterator();
            HashMap map = new HashMap();

            while (true) {
                while (it.hasNext()) {
                    VisitableElement e = (VisitableElement) it.next();
                    if (e instanceof TextElement) {
                        it.remove();
                    } else {
                        XmlElement el = (XmlElement) e;
                        List<Attribute> as = el.getAttributes();
                        if (!as.isEmpty()) {
                            String col = null;
                            Iterator i$ = as.iterator();

                            while (i$.hasNext()) {
                                Attribute a = (Attribute) i$.next();
                                if (a.getName().equalsIgnoreCase("column")) {
                                    col = a.getValue();
                                    break;
                                }
                            }

                            if (col != null) {
                                if (aliasLen > 0) {
                                    col = col.substring(aliasLen);
                                }

                                Optional<IntrospectedColumn> ic = introspectedTable.getColumn(col);
                                if (ic != null) {
                                    StringBuilder sb = new StringBuilder();
                                    IntrospectedColumn icol = ic.get();
                                    if (icol.getRemarks() != null && icol.getRemarks().length() > 1) {
                                        sb.append("<!-- ");
                                        sb.append(icol.getRemarks());
                                        sb.append(" -->");
                                        map.put(el, new TextElement(sb.toString()));
                                    }
                                }
                            }
                        }
                    }
                }

                if (map.isEmpty()) {
                    return;
                }

                Set<VisitableElement> set = map.keySet();
                Iterator i$ = set.iterator();

                while (i$.hasNext()) {
                    VisitableElement e = (VisitableElement) i$.next();
                    int id = es.indexOf(e);
                    es.add(id, (VisitableElement) map.get(e));
                    /**
                     * 注释，不生成行间隔 es.add(id, new TextElement(""));
                     */

                }
                es.add(new TextElement(""));
                return;
            }
        }
    }

    public static void delComment(XmlElement element) {
        List<VisitableElement> es = element.getElements();
        Iterator<VisitableElement> it = es.iterator();
        StringBuffer sb = new StringBuffer();
        while (it.hasNext()) {
            VisitableElement e = it.next();
            if (e instanceof TextElement) {
                TextElement t = (TextElement) e;
                String content = t.getContent();
                if (content.contains("<!--")) {
                    it.remove();
                }
                if (content.contains("MyBatis Generator")) {
                    it.remove();
                }
                if (content.contains("@mbg.generated")) {
                    it.remove();
                }
                if (content.contains("This element was generated on")) {
                    it.remove();
                }
                if (content.contains("-->")) {
                    it.remove();
                }
            }
        }
    }

}
